
import requests
import time
import re
from selenium import webdriver



driver = webdriver.Chrome()  # 实例化一个浏览器对象
driver.get('https://www.douyin.com/user/MS4wLjABAAAAUXBNFnWLvl9T8ylgAbD1auR_o5FL3dF7ic0KMYN9_88')
driver.implicitly_wait(10)


def drop_down():
    for x in range(1, 30, 4):
        time.sleep(1)
        j = x / 9
        js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j
        driver.execute_script(js)

def main():
    list_1 = [1, 2, 3, 4, 5, 6, 7]
    list_1[1:]
    drop_down()
    lis = driver.find_elements_by_css_selector(
        'div.mwbaK9mv > div:nth-child(2) > ul .ECMy_Zdt')  # 通过 css 选择器查找元素  获取多个li标签返回列表
    url_list = [li.find_element_by_css_selector('a').get_attribute('href') for li in lis]
    for li in lis:
        try:
            time.sleep(1)
            url = li.find_element_by_css_selector('a').get_attribute('href')
            # url = 'https://www.douyin.com/video/7087141617865346304'  # 网址
            headers = {
                'cookie': 'douyin.com; ttcid=444dfe8e89ff4d99b0662076ad171c8775; ttwid=1%7CTnFKlrGi3lHjKf5bshFdP9Nwu_Vsiwo-TxvX9NISgj8%7C1642083887%7Cfbfa904ea2900763eb6ac090bdd09014d80840da1ca485bbfea193d5401b330e; MONITOR_WEB_ID=c27b9f4a-4917-4256-be93-e948308467e3; odin_tt=0510c3c4196f54b541a96ac64e8b585b3a755be85057da8a1f3fa068e3f7b75ca2de4345e2b856f1e7b3f9455d86079731fe7d07a9f10890f26855d3674858e1; passport_csrf_token=e0b90cb756903c370592bd558c2b0cf5; passport_csrf_token_default=e0b90cb756903c370592bd558c2b0cf5; s_v_web_id=verify_l268jj46_kc7yYkD6_YHWW_4x4v_9snI_EDE0zro77uRn; AVATAR_FULL_LOGIN_GUIDE_COUNT=1; AVATAR_FULL_LOGIN_GUIDE_TIMESTAMP=1650982839652; AVATAR_FULL_LOGIN_GUIDE_ITA_COUNT=1; AVATAR_FULL_LOGIN_GUIDE_ITA_TIMESTAMP=1650982839652; __ac_nonce=0627ba36600d465d72261; __ac_signature=_02B4Z6wo00f01zrB8EAAAIDCWcswKSh.eLM65fTAAKzW8srQpmSjmL6YX9IsdmMSL4a9EBuyJvIwNMROqFQktniG-Ur-UDPK6wHInC8QKqRYUmyGnflwUXLpKzPgVt2FtREyprGmCDAZLrIpcc; douyin.com; strategyABtestKey=1652269927.635; AB_LOGIN_GUIDE_TIMESTAMP=1652269927510; AVATAR_LOGIN_GUIDE_COUNT=1; _tea_utm_cache_2285=undefined; _tea_utm_cache_6383=undefined; _tea_utm_cache_1300=undefined; pwa_guide_count=3; IS_HIDE_THEME_CHANGE=1; THEME_STAY_TIME=299808; msToken=XGPVAVUHDi9iTEQRjdXuQ0YyetxhHq0c9EH1dLLpttanbCXsNSD0DRxwk9oUB0vZ7LB9vKd-ABi2kAkzj2lCn1x98lJ4iTFbf260RcLav-G4QkhNyq8qV9i3oEJRyc8t; home_can_add_dy_2_desktop=1; msToken=3ALqenaebbJHw7kQDiDG6aRAgVYm5WM1pVGqmyyidbGgYpWRWKn-wQ9tcjoxWrHvwcqoYAx3tQ4IGE1qixdq2ei_fPrirMeeI6HeooU3sGR2wyWQ2OAAh2RejVJOrmpA; tt_scid=Gp0q0JW0LDreTqplgpajIZNHCB0.p1NcVv0hhZBgaGDw4SFxkXGlXfKafiCVmWAWc537',
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36'
            }
            response = requests.get(url=url, headers=headers)
            # print(url)
            # <Response [200]> 响应对象 200 状态码 表示请求成功 response.text 获取响应对象文本数据
            # print(response.text)
            """
            数据解析, 提取我们想要数据内容
                re正则表达式 <在付费课程 2.5个小时的内容讲解完>
            re.findall() 调用re模块里面findall方法 去查询匹配数据
                找到所有 >>> 从什么地方去找什么样数据 (.*?) 表示你想要数据内容, 通配符, 可以匹配任意字符(除了换行符以外)
            """
            title = re.findall('<title data-react-helmet="true">(.*?)</title>', response.text, re.S)[0]
            title = re.sub(r'[/\:*?"<>|\n]', '', title)
            video_url = re.findall('src(.*?)%22%7D%2C%7B%22src', response.text)[0]  # 编码的内容获取
            video_url_1 = requests.utils.unquote(video_url).replace('":"', 'https:')  # 解码
            # 编码 requests.utils.quote
            # 就业工作 1 接单赚钱 2
            # print(title)
            # print(video_url)
            # print(video_url_1)
            video_content = requests.get(url=video_url_1, headers=headers).content  # 发送请求获取二进制数据内容
            with open('img\\' + title + '.mp4', mode='wb') as f:
                f.write(video_content)  # 写入内容
                print('正在保存: ', title)
        except Exception as e:
            print(e)


if __name__ == '__main__':
    main()